import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { DNavBar } from '@/components'
import { AtButton, AtInput } from 'taro-ui'
import { setToken } from '@/http/http.js'
import './home.scss'

export default class HomeBox extends Component {

    constructor(props) {
        super(props)
        this.state = {
            userToken: "",
        }
    }

    componentWillMount() {
        // Taro.showToast({
        //     title: "第一个"
        // })
        // let { userToken } = this.state
        // userToken = localStorage.getItem('X-Auth0-Token')
        // this.setState({
        //     userToken
        // })
    }

    getIOS() {
        // let _this = this
        // Taro.showToast({
        //     title: "第二个",
        //     icon: 'none',
        //     duration: 800
        // })
        // alert("windows" + window.bridge)
        // window.bridge.callHandler('openCamera', {
        //     'count': '10张'
        // }, function responseCallback(responseData) {
        //     _this.setState({
        //         secondText: responseData.url
        //     })
        //     alert("reps-->" + responseData.url)
        //     console.log("OC中返回的参数:" + responseData)
        // })
    }

    getToken(value) {
        this.setState({
            userToken: value
        })
    }

    setToken() {
        let { userToken } = this.state
        if (!userToken) {
            return Taro.showToast({
                title: "请输入Token值",
                icon: "none"
            })
        }
        localStorage.setItem('X-Auth0-Token', userToken)
        Taro.showToast({
            title: "设置成功"
        }).then(() => {
            // Taro.navigateBack({
            //     delta: 1
            // })
            setToken(userToken)
            setTimeout(() => {
                Taro.redirectTo({
                    url: `/pages/demo/demo`
                })
            }, 2000)
        })
    }


    render() {
        const {
            userToken
        } = this.state
        return (
            <View className='b_home'>
                <DNavBar title='设置'></DNavBar>
                <View className='b_home__box'>
                    <AtInput title='设置Token' value={userToken} onChange={this.getToken.bind(this)} placeholder='请输入Token值'></AtInput>
                    {
                        userToken && <View className='b_home__token'>{userToken}</View>
                    }
                    <AtButton className='item_btn' onClick={this.setToken.bind(this)}>设置Token</AtButton>
                </View>
            </View>
        )
    }
}